package com.hw.controller;

import com.github.pagehelper.PageInfo;
import com.hw.dto.R;
import com.hw.pojo.Areas;
import com.hw.service.AreasService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * @ClassName: AreaController
 * @Description:
 * @Author: vie0134
 * @Date: 2020-12-15 16:31
 * @Version: 1.0
 **/
@RestController
@RequestMapping("/areas")
public class AreaController {
    @Autowired
    private AreasService areasService;

    @RequestMapping("/findAllArea")
    public R findAllArea(@RequestParam(defaultValue = "1") int page,
                         @RequestParam(defaultValue = "10") int limit,
                         String exludeId, String areaname,
                         @RequestParam(defaultValue = "-100") String status) {
        PageInfo<Areas> pageInfo = areasService.findAllArea(page, limit,exludeId, areaname, status);
        return R.setSuccess(pageInfo);
    }

    @PutMapping("/area")
    public R addAreas(@RequestBody Areas areas) {
        areasService.addAreas(areas);
        return R.setSuccess(null);
    }

    @PostMapping("/area")
    public R updateAreas(@RequestBody Areas areas) {
        areasService.updateAreas(areas);
        return R.setSuccess(null);
    }

    @DeleteMapping("/deleteAreasById")
    public R deleteAreasById(Integer id) {
        areasService.deleteAreasById(id);
        return R.setSuccess(null);
    }

    @DeleteMapping("/area")
    public R deleteAreasByIds(String[] ids) {
        areasService.deleteAreasByIds(Arrays.asList(ids));
        return R.setSuccess(null);
    }

    @GetMapping("/area/{id}")
    public R findAreasById(@PathVariable Integer id) {
        Areas area = areasService.findAreasById(id);
        return R.setSuccess(area);
    }

    @GetMapping("/area/parent/{id}")
    public R findAreasByParentId(@PathVariable Integer id) {
        List<Areas> areasList = areasService.findAreasByParentId(id);
        return R.setSuccess(areasList);
    }

    @GetMapping("/areas/parent")
    public R findFirstParentAreas(@RequestParam(defaultValue = "-100") String status) {
        List<Areas> areasList = areasService.findFirstParentAreas(status);
        return R.setSuccess(areasList);
    }

}
